//
//  NPNavigationController.m
//  iOS_Obj_BaseFrameWork
//
//  Created by Mr nie on 16/3/16.
//  Copyright © 2016年 程序猿:NiePlus   博客地址:http://nieplus.blog.com. All rights reserved.
//

#import "ZXNavigationController.h"

@interface ZXNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@end

@implementation ZXNavigationController


- (void)viewDidLoad
{
    __weak ZXNavigationController *weakSelf = self;
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
        self.delegate = weakSelf;
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //判断self.childViewControllers是不是拿到所有子控制器，self.childViewControllers.count为0时，只有子控制器，干掉返回按钮
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = NO;
    
    if (self.childViewControllers.count > 0) {
        //        //自定义返回按钮
        //        UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //        //设置title:返回
        ////        [returnBtn setTitle:@"返回" forState:UIControlStateNormal];
        //        //正常下image
        //        [returnBtn setImage:[UIImage imageNamed:@"black"] forState:UIControlStateNormal];
        //        //高亮下image
        //        [returnBtn setImage:[UIImage imageNamed:@"grat"] forState:UIControlStateHighlighted];
        //        //正常下title颜色
        //        [returnBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //        //高亮下title颜色
        //        [returnBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        //        //设置按钮位置大小,注意这里位置无效果
        //        [returnBtn setFrame:CGRectMake(0, 0, 70, 20)];
        //        //按钮内部对齐
        //        [returnBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        //        //设置图片内部位置
        //        [returnBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        //        //添加点击事件
        //        [returnBtn addTarget:self action:@selector(returnBtnClick) forControlEvents:UIControlEventTouchUpInside];
        //当视图推送时隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        //        //自定义push视图的leftBarButtonItem
        //        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnBtn];
    }
    //子控制器通过这个方法推送视图
    [super pushViewController:viewController animated:animated];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)returnBtnClick
{
    [self popViewControllerAnimated:YES];
}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
    // Enable the gesture again once the new controller is shown
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = YES;
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    
    return [super popViewControllerAnimated:YES];
}

#pragma mark UINavigationControllerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([self.childViewControllers count] == 1) {
        return NO;
    }
    return YES;
}

// 我们差不多能猜到是因为手势冲突导致的，那我们就先让 ViewController 同时接受多个手势吧。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
//运行试一试，两个问题同时解决，不过又发现了新问题，手指在滑动的时候，被 pop 的 ViewController 中的 UIScrollView 会跟着一起滚动，这个效果看起来就很怪（知乎日报现在就是这样的效果），而且也不是原始的滑动返回应有的效果，那么就让我们继续用代码来解决吧
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
